package cn.core.autobuilder.modules.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.core.autobuilder.dao.mapper.Builder_codeMapper;
import cn.core.autobuilder.dao.mapper.Builder_tableMapper;
import cn.core.autobuilder.dao.po.Builder_codePO;
import cn.core.autobuilder.dao.po.Builder_tablePO;
import cn.core.autobuilder.service.Builer_codeService;
import cn.osworks.aos.core.asset.AOSJson;
import cn.osworks.aos.core.asset.AOSUtils;
import cn.osworks.aos.core.asset.WebCxt;
import cn.osworks.aos.core.dao.SqlDao;
import cn.osworks.aos.core.excel.POIExcelUtil;
import cn.osworks.aos.core.typewrap.Dto;
import cn.osworks.aos.core.typewrap.Dtos;
import cn.osworks.aos.core.typewrap.impl.HashDto;

/**
 * @author penghe  hepeng1@163.com
 *
 */
@Controller
@RequestMapping(value = "autobuilder/builder")
public class Builer_codeController {
	@Autowired
	private Builer_codeService builer_codeservice;
	@Autowired
	private SqlDao sqlDao;
	@Autowired
	private Builder_codeMapper builder_codemapper;
	@Autowired
	private Builder_tableMapper builder_tablemapper;
	

	@RequestMapping(value = "init")
	public String init(HttpSession session, HttpServletRequest request) {
		return "core/autobuilder/builer_code.jsp";
	}

	@RequestMapping(value = "list")
	public void list(HttpServletRequest request, HttpServletResponse response) {
		Dto inDto = Dtos.newDto(request);
		HttpSession session = request.getSession();
		if (session != null)
			session.setAttribute("builer_code", inDto);
		List<Builder_codePO> list = builder_codemapper.likePage(inDto);
		//List<Builder_codePO> list = sqlDao.list("Builder_code.listBuilder_code", inDto);
		String outString = AOSJson.toGridJson(list, inDto.getPageTotal());
		WebCxt.write(response, outString);
	}
	@RequestMapping(value = "save")
	public void save(HttpServletRequest request, HttpServletResponse response) {
		Dto dto = Dtos.newDto(request);
		Dto outDto =builer_codeservice.saveBuiler_code(dto);
		WebCxt.write(response, AOSJson.toJson(outDto));
	}
	@RequestMapping(value = "update")
	public void update(HttpServletRequest request, HttpServletResponse response) {
		Dto dto = Dtos.newDto(request);
		Dto outDto =builer_codeservice.updateBuiler_code(dto);
		WebCxt.write(response, AOSJson.toJson(outDto));
	}
	@RequestMapping(value = "delete")
	public void delete(HttpServletRequest request, HttpServletResponse response) {
		Dto dto = Dtos.newDto(request);
		Dto outDto = builer_codeservice.delete(dto);
		WebCxt.write(response, AOSJson.toJson(outDto));
	}
	@RequestMapping(value = "importtableinfo")
	public void importtableinfo(HttpServletRequest request, HttpServletResponse response) {
		Dto dto = Dtos.newDto(request);
		Dto outDto =builer_codeservice.importtableinfo(dto);
		WebCxt.write(response, AOSJson.toJson(outDto));
	}
	
	@RequestMapping(value = "importtableinfos")
	public void importtableinfos(HttpServletRequest request, HttpServletResponse response) throws SQLException {
		Dto dto = Dtos.newDto(request);
		Dto outDto =builer_codeservice.importtableinfos(dto);
		WebCxt.write(response, AOSJson.toJson(outDto));
	}
	
	@RequestMapping(value = "tableinfo")
	public void tableinfo(HttpServletRequest request, HttpServletResponse response) {
		Dto inDto = Dtos.newDto(request);
		Builder_tablePO builder_tablePo = builder_tablemapper.selectOne(inDto);
		String outString = AOSJson.toJson(builder_tablePo);
		WebCxt.write(response, outString);
	}
	
	@RequestMapping(value = "updatetableinfo")
	public void updateTableinfo(HttpServletRequest request, HttpServletResponse response) {
		Dto dto = Dtos.newDto(request);
		Dto outDto =builer_codeservice.updateBuiler_table(dto);
		WebCxt.write(response, AOSJson.toJson(outDto));
	}
	
	@RequestMapping(value = "listBuilder_table")
	public void listBuilder_Table(HttpServletRequest request, HttpServletResponse response) {
		Dto inDto = Dtos.newDto(request);
		HttpSession session = request.getSession();
		if (session != null)
			session.setAttribute("builer_code", inDto);
		List<Builder_tablePO> list = builder_tablemapper.list(inDto);
		String outString = AOSJson.toGridJson(list);
		WebCxt.write(response, outString);
	}
	
	@RequestMapping(value = "exportexcel")
	public void exportexcel(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
	    Dto dto = (HashDto) session.getAttribute("builer_code");
		String filename="";
		try {
	    filename = URLEncoder.encode("builer_code", "utf-8");
		InputStream bis = null;
		BufferedInputStream buffis = null;
		OutputStream fos = null;
		BufferedOutputStream bos = null;
		fos = response.getOutputStream();
		bos = new BufferedOutputStream(fos);
		String[] titleHead=new String[] { "table_name_","一对一column_name_数"};
		String[] datarow=new String[] {"table_name_","column_name_"};
		List<Builder_codePO> list = builder_codemapper.likePage(dto);
		List<Dto> outlist = new ArrayList<Dto>();
		for(int i=0;i<list.size();i++){
			Builder_codePO builder_codePO =  list.get(i);
			Dto dataDto = new HashDto();
			AOSUtils.copyProperties(builder_codePO, dataDto);
			outlist.add(dataDto);
		}
		bis = POIExcelUtil.create(filename, outlist,titleHead , datarow); 
		buffis = new BufferedInputStream(bis);
		response.setContentType("application/xls");
		response.setHeader("Content-Disposition", "inline;filename=" + URLEncoder.encode(filename + ".xls", "utf-8"));
		int bytesRead = 0;
		byte[] buffer = new byte[1024];
		while ((bytesRead = buffis.read(buffer, 0, buffer.length)) != -1) {
			bos.write(buffer, 0, bytesRead);
		}
		bos.flush();
		bis.close();
		buffis.close();
		fos.flush();
		fos.close();
		bos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}
}
